package com.tenglong.teacher.ui.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tenglong.teacher.R;

public class TextLayout extends LinearLayout implements View.OnFocusChangeListener {
    private TextView labelView;
    private ImageView pswView;
    private EditText editor;
    private boolean mClick;

    public TextLayout(Context context) {
        this(context,null);
    }

    public TextLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);

    }


    public void setText(String str){
        editor.setText(str);
    }
    public void setInputType(int type){
        editor.setInputType(type);
    }
    public String getText(){
        return editor.getText().toString();
    }


    private void init(Context context, AttributeSet attrs){
        LayoutInflater.from(context).inflate(R.layout.text_layout, this);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextLayout);
        int label = typedArray.getResourceId(R.styleable.TextLayout_label,0);
        labelView=findViewById(R.id.label);
        labelView.setText(label);
        pswView=findViewById(R.id.right_icon);
        editor=findViewById(R.id.editor);
        this.setBackgroundResource(R.drawable.input_unfoucs);
        int hint=typedArray.getResourceId(R.styleable.TextLayout_hint,0);
        if (hint != 0){
            editor.setHint(hint);
        }
        if(typedArray.getBoolean(R.styleable.TextLayout_ischoose, true)){
            pswView.setVisibility(VISIBLE);
            editor.setInputType(InputType.TYPE_NULL);
            editor.setFocusable(false);
        }else{
            editor.setOnFocusChangeListener(this);
            pswView.setVisibility(GONE);
        }
        mClick=typedArray.getBoolean(R.styleable.TextLayout_clickf,true);
        if(!mClick){
            setClickable(true);
            editor.setClickable(false);
            editor.setDuplicateParentStateEnabled(true);
            editor.setFocusable(false);
            editor.setFocusableInTouchMode(false);
            editor.setEnabled(false);
            labelView.setClickable(false);
            // labelView.setDuplicateParentStateEnabled(true);

        }
    }

    public void setEdiHint(String hint){
        editor.setHint(hint);
    }

    public void setEdiHint(int hint){
        editor.setHint(hint);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(!mClick){
            return true;
        }else{
            return super.onInterceptTouchEvent(ev);
        }


    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if(view.getId()==R.id.editor){
            if(b){
                this.setBackgroundResource(R.drawable.input_focus);
                //((TextView)view).setTextColor(0xff0079e5);
            }else{
                this.setBackgroundResource(R.drawable.input_unfoucs);
                //((TextView)view).setTextColor(0xff000000);
            }
        }
    }

}
